/* jshint esversion: 6*/
import req from "./req";
import url from "./url";
import utils from "./utils";
export default {
    state: {
        id: '',
        enrollee: '',
        users: [],
        addressList: [],
        meetingList: [],
        noticeList: [],
        scheduleList: [],
        messageList: [],
        worklogList: [],
        address: {},
        meeting: {},
        notice: {},
        schedule: {},
        message: {},
        worklog: {},
        pageArray: [],
        key: 0
    },
    mutations: {
        initUser(state) {
            req
                .POST(url.user.list, null)
                .then(data => {
                    state.users = data;
                });
            for (let i = 0; i < state.users.length; i++) {
                if (state.users[i].id == state.id) {
                    state.users.splice(i, 1);
                    break;
                }
            }
        },
        initAddress(state) {
            req
                .POST(url.address.list, req.myListParam({ username: state.enrollee }))
                .then(data => {
                    state.addressList = data;
                });
        },
        initMeeting(state) {
            req
                .POST(url.meeting.list, null)
                .then(data => {
                    state.meetingList = data;
                });
        },
        initNotice(state) {
            req
                .POST(url.notice.list, null)
                .then(data => {
                    state.noticeList = data;
                });
        },
        initSchedule(state) {
            req
                .POST(url.schedule.list, req.myListParam({ username: state.enrollee }))
                .then(data => {
                    state.scheduleList = data;
                });
        },
        initMessage(state) {
            req
                .POST(url.message.list, req.myListParam({ username: state.enrollee }))
                .then(data => {
                    state.messageList = data;
                });
        },
        initWorklog(state) {
            req
                .POST(url.worklog.list, req.myListParam({ username: state.enrollee }))
                .then(data => {
                    state.worklogList = data;
                });
        },
        init(state, param) {
            state.id = param.id;
            state.enrollee = param.username;
            this.commit('initUser');
            this.commit('initAddress');
            this.commit('initMeeting');
            this.commit('initNotice');
            this.commit('initSchedule');
            this.commit('initMessage');
            this.commit('initWorklog');
        },
        select(state, param) {
            state[param.name] = {};
            state[param.name] = state[utils.getLocalArrayName(param.name)][param.index];
            state.index = param.index;
        }, setPageArray(state, array) {
            state.pageArray=array;
        }
    }, getters: {

    }, actions: {

    }
};